﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ClientLib
{
    public partial class LoginForm : Form
    {
        public TinyMsgToolLib.TinyMsgInterface TCPclient;
        public static ClientLib.TinyClientRuntime m_runtime;
        private string m_RemoteIP;
        private int m_RemotePort;
        public const int LOGINFORM_LOGINSUCC = 1;
        public const int LOGINFORM_USERORPASSWRODERROR = 2;
        public const int LOGINFORM_SERVERERROR = 3;
        private int m_state = 0;
        public int getLoginStatus()
        {
            return m_state;
        }
        public void setLoginStauts(int nextState)
        {
            m_state = nextState;
        }
        public void setRuntime(ClientLib.TinyClientRuntime runtime)
        {
            m_runtime = runtime;
            TCPclient = m_runtime.g_TCPclient;
        }
        
        public bool setRemoteAddr(string remoteip, int remoteport)
        {
            if (remoteip == "")
                return false;
            else
            {
                m_RemoteIP = remoteip;
                m_RemotePort = remoteport;
                return true;
            }
        }
        public LoginForm()
        {
            InitializeComponent();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            if (TCPclient.connect(m_RemoteIP, m_RemotePort) == false)
            {
                MessageBox.Show("服务器连接失败");
                return;
            }
            if (textBoxUserName.Text.Length < 1 || textBoxPassword.Text.Length < 1)
            {
                MessageBox.Show("用户名或者密码不允许为空");
                return;
            }
            else
            {

                //login process

                m_runtime.g_Config.m_ModuleName = "helloworld1";
                m_runtime.g_Config.m_ModuleType = "TinyClient";
                m_runtime.g_Config.m_region = "127.0.0.1";
                m_runtime.g_Config.m_userName = textBoxUserName.Text;

                TCPclient.setMsgIndex(GlobalDefine.CLIENTMGR);
                TCPclient.setMsgItem(GlobalDefine.CLIENTLOGON_REQUEST);
                TCPclient.setDefaultParamByIndex(GlobalDefine.CLIENTLOGON_MODULENAME, "helloworld1");
                TCPclient.setDefaultParamByIndex(GlobalDefine.CLIENTLOGON_MODULETYPE, "TinyClient");
                TCPclient.setDefaultParamByIndex(GlobalDefine.CLIENTLOGON_USERNAME, textBoxUserName.Text);
                TCPclient.setDefaultParamByIndex(GlobalDefine.CLIENTLOGON_PASSWORD, textBoxPassword.Text);
                TCPclient.setDefaultParamByIndex(GlobalDefine.CLIENTLOGON_REGION, "127.0.0.1");


                //file param
                if (TCPclient.talk() == true &&
                    TCPclient.getMsgIndex() == GlobalDefine.CLIENTMGR && TCPclient.getMsgItem() == GlobalDefine.CLIENTLOGON_SUCC)
                {
                    //get param
                    setLoginStauts(LOGINFORM_LOGINSUCC);
                    //TCPclient.shutdown();
                    Close();
                }
                else
                {
                    //logon failed!
                }
            }
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            comboBoxServerAddr.Text = m_RemoteIP;
            comboBoxServerAddr.Enabled = false;
        }
    }
}